package com.softtown.modules.sys.service.impl;

import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.common.utils.HttpContextUtils;
import com.softtown.common.utils.TreeUtils;
import com.softtown.modules.sys.dao.SysGuestMenuDao;
import com.softtown.modules.sys.dao.SysMenuDao;
import com.softtown.modules.sys.dto.SysMenuDTO;
import com.softtown.modules.sys.entity.SysGuestMenuEntity;
import com.softtown.modules.sys.entity.SysMenuEntity;
import com.softtown.modules.sys.enums.MenuTypeEnum;
import com.softtown.modules.sys.service.SysGuestMenuService;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 游客菜单
 *
 * @author Dingyayong
 * @version v1.0
 * @create 2019-06-25 10:39
 **/
@Service
public class SysGuestMenuServiceImpl extends BaseServiceImpl<SysGuestMenuDao, SysGuestMenuEntity> implements SysGuestMenuService {

    @Resource
    private SysMenuDao sysMenuDao;

    @Override
    public List<SysMenuDTO> getGusetAppMenus() {
        List<SysGuestMenuEntity> sysGuestMenuEntities = baseDao.selectMenus();
        List<Long> menuIds = new ArrayList<>();
        Date nowDate = new Date();
        for (SysGuestMenuEntity sysGuestMenuEntity: sysGuestMenuEntities) {
            if (nowDate.before(sysGuestMenuEntity.getExprDate())) {
                menuIds.add(sysGuestMenuEntity.getMenuId());
            }
        }
        List<SysMenuEntity> sysMenuEntities = sysMenuDao.selectListByIds(menuIds, MenuTypeEnum.APP.value(), HttpContextUtils.getLanguage());
        return ConvertUtils.sourceToTarget(sysMenuEntities, SysMenuDTO.class);
    }

    @Override
    public List<SysMenuDTO> getGuestNav(Long appId) {
        List<SysMenuEntity> sysMenuEntities = baseDao.getGuestMenuList(MenuTypeEnum.MENU.value(), HttpContextUtils.getLanguage());
        List<SysMenuDTO> dtoList = ConvertUtils.sourceToTarget(sysMenuEntities, SysMenuDTO.class);
        // 获取pid为指定AppId
        return TreeUtils.build(dtoList).stream()
                .filter(sysMenuDTO -> sysMenuDTO.getPid().equals(appId))
                .collect(Collectors.toList());
    }
}

